<div style="padding:20px;" id="container_d_#data.data_id">
<? $defaultBespinOptions = ', "settings": {"fontsize": 10, "tabstop": 4, "theme": "white", "codecomplete": true, "autoindent": true}, "stealFocus": true'; ?>
<form id="hformd<?=(int)$arr_param["data"]["data_id"]?>" method="post" action="?event=builder:listModules" onsubmit="$('hCoded<?=(int)$arr_param["data"]["data_id"]?>').value=phpEditord<?=(int)$arr_param["data"]["data_id"]?>.getCode();invoke(null, 'builder:editData&check=1', this.serialize(), true, function(req) {loadURL('?event=builder:listModules');});return false;">
	<label for="name">Name of Data Query</label><br/>
	<input id="d_name_#data.data_id" type="text" name="data[name]" value="#data.name" /><br/><br/>
	<input id="d_nameh_#data.data_id" type="hidden" value="#data.name" />

	<label for="coded<?=(int)$arr_param["data"]["data_id"]?>">Data Code:</label><br/>
	<input type="hidden" rel="coded<?=(int)$arr_param['data']['data_id']?>" id="hCoded<?=(int)$arr_param["data"]["data_id"]?>" name="data[code]" value="" />
	<div style="border:1px solid #ccc;" rel="builder:editData&refresh=code&data_id=<?=(int)$arr_param["data"]["data_id"]?>" id="coded<?=(int)$arr_param["data"]["data_id"]?>" data-bespinoptions='{"syntax": "php"<?=$defaultBespinOptions?>}'>#data.code</div>
	<div style="clear:both;"></div>
	<br/>
	<script type="text/javascript">
	//<![CDATA[
		window.keywords = [	"list", "foreach", "array", "in", "include", "die", "echo", "empty", "exit", "eval", "include_once", "isset",
							"require", "require_once", "return", "print", "unset", "case", "class", "abstract", "and", "as", "break",
							"catch", "clone", "const", "continue", "declare", "default", "do", "else", "elseif", "enddeclare", "endfor",
							"endforeach", "endif", "endswitch", "endwhile", "extends", "final", "for", "foreach", "function", "global",
							"goto", "if", "implements", "interface", "instanceof", "namespace", "new", "or", "private", "protected",
							"public", "static", "switch", "throw", "try", "use", "var", "while", "xor"
		];
	
		setTimeout(function() {
			$("container_d_#data.data_id").save = function() {
  	    	    if (this.saving) return;
	    	    this.saving = true;
				document.title = "Saving data query "+$("d_name_#data.data_id").getValue()+"...";
				if (window.keywords.indexOf($("d_name_#data.data_id").getValue()) >= 0) {
					$("d_name_#data.data_id").value = "_"+$("d_name_#data.data_id").getValue();
				}
				$('hCoded<?=(int)$arr_param["data"]["data_id"]?>').value = Builder.getCode("coded<?=(int)$arr_param["data"]["data_id"]?>");
				var me = this;
				invoke(null, 'builder:editData&check=1&module_id=<?=if_set($arr_param['data']['fk_module_id'], $_SESSION["module_id"])?>&data_id=<?=(int)$arr_param['data']['data_id']?>', $('hformd<?=(int)$arr_param["data"]["data_id"]?>').serialize(), true, function(req) {
					me.saving = false;
					document.title = "Prails Home";
					Ext.ux.util.msg("Saving completed.", "Your changes have been saved successfully.");

					$("coded<?=(int)$arr_param["data"]["data_id"]?>").crc.content = Builder.getCode("coded<?=(int)$arr_param["data"]["data_id"]?>");
					$("coded<?=(int)$arr_param["data"]["data_id"]?>").dirty = false;
					
					if (<?=(int)$arr_param["data"]["data_id"]?> == 0) {
						var name = $("d_name_#data.data_id").getValue();
						var id = req.responseText.split(/\s/g)[0];
						var mid = <?=(int)$_SESSION["module_id"]?>;
						var module = Builder.root.findChild("id", mid);
						Builder.closeTab("d_0");
						var n = new Ext.tree.TreeNode({
							text: name,
							leaf: true,
							id: "d_"+id,
							allowChildren: false
						});
						n.cls = "query";
						module.datas.push(n);
						Builder.editData(n);
						Builder.resetTree(Builder.dataRoot, module.datas);
					}
				});
				return false;			
			};			
			Builder.applyBespin("coded<?=(int)$arr_param["data"]["data_id"]?>", function(fr) {
				$("coded<?=(int)$arr_param['data']['data_id']?>").down("iframe").up().style.height = (parseInt($("hformd<?=(int)$arr_param['data']['data_id']?>").parentNode.parentNode.style.height) - 150) + "px";
        		fr.txt.addKeywords(null, function(txt) {
        			var res = [];
        			var matches = txt.value.substr(0, txt.selectionEnd).match(/\$[a-zA-Z_][a-zA-Z_0-9]*\b/gi);
        			if (matches) {
	        			for (var a = 0; a < matches.length; a++) {
	        				if ((","+res.join(",")+",").indexOf(matches[a]) < 0) {
	        					res.push(matches[a]);
	        				}
	        			}
        			}
        			return res;
        		});

				fr.txt.addKeywords("\\$this->", ["query", "add", "set", "update", "get", "select", "ins", "insert", "remove", "del", "delete", "update", "edit"]);
		      	setTimeout(function() {
						$('hCoded<?=(int)$arr_param["data"]["data_id"]?>').value = Builder.getCode("coded<?=(int)$arr_param["data"]["data_id"]?>");				
		      	}, 1000);				
			}, Object.extend(Builder.prailsNamespace, {
	    		id: "container_d_#data.data_id",
				save: function() {
		        	parent.$("container_d_#data.data_id").save();
				}
	      	}));
	      	
			Ext.getCmp("portlet_content_d_<?=(int)$arr_param['data']['data_id']?>").getTopToolbar().add({
				xtype: "button",
				text: "Save",
				iconCls: "save",
				handler: function(e) {
					$("container_d_#data.data_id").save();
				}
			}, "-", {
				xtype: "button",
				text: "SQL Query",
				iconCls: "run",
				handler: function(e) {
					Builder.queryTest();
				}
			},"-", {
				xtype: "button",
				text: "Debug",
				iconCls: "debug",
				disabled: (<?=(int)$arr_param["data"]["data_id"]?> <= 0 ? true : false),
				handler: function(e) {
					Builder.debug('#data.fk_module_id', 'data', '#data.data_id');
				}
			}, "-", {
				xtype: "button",
				id: "data.#data.data_id_viewHistory.button",
				text: "View History",
				iconCls: "history",
				disabled: (<?=(int)$arr_param["data"]["data_id"]?> == 0 ? true : false),
				handler: function(e) {
					if ($("d_#data.data_id") != null) {
						$("d_#data.data_id").fisheye.dispose();
						$("d_#data.data_id").remove();
						Ext.getCmp("data.#data.data_id_viewHistory.button").setText("View History");
					} else {
						Builder.browseDataHistory(<?=(int)$arr_param["data"]["data_id"]?>);
						Ext.getCmp("data.#data.data_id_viewHistory.button").setText("Leave History");
					}
				}
			}/*, "-", {
				xtype: "button",
				text: "Model View",
				id: "data.#data.data_id_viewModel.button",
				iconCls: "model",
				handler: function(e) {
					new Ajax.Request("?event=builder:dataModel&data_id=#data.data_id", {
						evalJS: true,
						onSuccess: function(req) {
							$("container_d_#data.data_id").update(req.responseText);
						}
					});
				}
			}*/);	
			Ext.getCmp("portlet_content_d_<?=(int)$arr_param['data']['data_id']?>").getTopToolbar().doLayout();
		}, 100);
	//]]>
	</script>
</form>
</div>